package com.comcast.core.eeg.eventpublication.ejb;

import java.rmi.RemoteException;


import javax.ejb.CreateException;
import javax.ejb.EJBException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import weblogic.ejbgen.Constants;
import weblogic.ejbgen.JarSettings;
import weblogic.ejbgen.JndiName;
import weblogic.ejbgen.LocalMethod;
import weblogic.ejbgen.MethodPermissionPattern;
import weblogic.ejbgen.RemoteMethod;
import weblogic.ejbgen.RoleMapping;
import weblogic.ejbgen.Session;

import com.comcast.core.eeg.eventpublication.types.PublicationOption;
import com.comcast.core.eeg.types.BusinessEvent;
import com.comcast.core.eeg.types.Event;
import com.comcast.core.eeg.types.Participant;
import com.comcast.core.eeg.types.Result;
import com.comcast.core.eeg.types.TransactionOption;
import com.comcast.util.ObjectFactory;

@Session(maxBeansInFreePool = "30",
         initialBeansInFreePool = "0",
         //transTimeoutSeconds = "360",
         type = Session.SessionType.STATELESS,
         transactionType = Session.SessionTransactionType.BEAN,
         //defaultTransaction = Constants.TransactionAttribute.NOT_SUPPORTED,
         enableCallByReference = Constants.Bool.TRUE,
         remoteClientTimeout = "60",
         ejbName = "EventPublicationServiceBean")

@JndiName(remote="com.comcast.core.eeg.eventpublication.EventPublicationServiceBean",
          local="com.comcast.core.eeg.eventpublication.LocalEventPublicationServiceBean")

@MethodPermissionPattern(pattern= "*", roles= "EEP_PUBLISHER")

@RoleMapping(roleName = "EEP_PUBLISHER", principals = "EepPublishers")

@JarSettings(ejbClientJar = "epejbsvc-1.0.0.0-client.jar")

public class EventPublicationServiceBean implements SessionBean {

	private static final long serialVersionUID = 1L;

    private static Log logger = LogFactory.getLog(EventPublicationServiceBean.class);

    private static final String UNKNOWN_ACTOR = "UNKNOWN";
    private static final String LABEL_DELIM = ":";
    
    //eeg event names
    private static final String Account = "Account";
    private static final String Device = "Device";
    private static final String AccountDeleted = "AccountDeleted";
    private static final String DeviceDeleted = "DeviceDeleted";
    private static final String OutletDeviceServices = "OutletDeviceServices";
    private static final String OutletDeviceServicesDeleted = "OutletDeviceServicesDeleted";
    private static final String LocationAdded = "LocationAdded";
    private static final String LocationUpdated = "LocationUpdated";
    private static final String LocationDeleted = "LocationDeleted";
    private static final String Location = "Location";
  
    private com.comcast.core.eeg.eventpublication.service.EventPublicationService epsvc = (com.comcast.core.eeg.eventpublication.service.EventPublicationService)ObjectFactory.getBean("EventPublicationService");

	private SessionContext ctx = null;

	public EventPublicationServiceBean() {
		super();
        logger.info("EventPublicationServiceBean has been created.");
	}

	public void setSessionContext(SessionContext context)
        throws EJBException, RemoteException {
		ctx = context;
	}

	public void ejbRemove() throws EJBException, RemoteException {}

	public void ejbActivate() throws EJBException, RemoteException {}

	public void ejbPassivate() throws EJBException, RemoteException {}

	public void ejbCreate() throws CreateException {}

    @RemoteMethod()
    public Result publish(Participant actor, BusinessEvent event, PublicationOption option) {
    	
    	if(logger.isDebugEnabled()) {
    		logger.debug("actor:"+actor+"-BusinessEvent:"+event+"-PublicationOption:"+option);
    	}
    	
        BusinessEvent[] events = null;
        if (event != null) {
            events = new BusinessEvent[]{event};
        }
        
		return batchPublish(actor, events, option);
	}

    @RemoteMethod()
    public Result batchPublish(Participant actor, BusinessEvent[] events, PublicationOption option) {
    	if(logger.isDebugEnabled()) {
    		logger.debug("actor:"+actor+"-BusinessEvent:"+events+"-PublicationOption:"+option);
    	}

    	if (option == null) {
            option = new PublicationOption();
        }

        long start = System.currentTimeMillis();
        //do not support distributed transaction, and local transaction
        option.setTransaction(TransactionOption.AUTO_COMMIT);
        Result result = epsvc.batchPublish(actor, events, option);

        //only deal with good case
        if (result.getSucceeded() && events != null && events.length != 0) {
        }

        return result;
    }

    @RemoteMethod()
    @LocalMethod()
    public Result publishEvent(Participant actor, Event event, PublicationOption option) {
    	if(logger.isDebugEnabled()) {
    		logger.debug("actor:"+actor+"-Event:"+event+"-PublicationOption:"+option);
    	}

        Event[] events = null;
        if (event != null) {
            events = new Event[]{event};
        }
        return publishEvents(actor, events, option);
    }

    @RemoteMethod()
    @LocalMethod()
    public Result publishEvents(Participant actor, Event[] events, PublicationOption option) {
    	
    	if(logger.isDebugEnabled()) {
    		logger.debug("actor:"+actor+"-Event:"+events+"-PublicationOption:"+option);
    	}

    	
        if (option == null) {
            option = new PublicationOption();
        }

        long start = System.currentTimeMillis();
        option.setTransaction(TransactionOption.AUTO_COMMIT);
        Result result = epsvc.batchPublish(actor, events, option);

        if (result.getSucceeded() && events != null && events.length != 0) {
        	
        }

        return result;
    }
    

}
